from playwright.sync_api import sync_playwright
import os
import sys
import time
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if project_root not in sys.path:
    sys.path.append(project_root)

from readlater.utils.logger import get_logger

# 启动服务
if __name__ == "__main__":
    logger = get_logger(__name__)

    url = "https://mp.weixin.qq.com/s?scene=1&__biz=MzI2NDU4OTExOQ==&mid=2247691800&idx=1&sn=4b4f6ef36250e6a2ba73277a63cfe360&sharer_shareinfo_first=591dc2035d0b7483bf461414b20b38a9&sharer_shareinfo=591dc2035d0b7483bf461414b20b38a9"
    with sync_playwright() as p:
        browser = p.firefox.launch(headless=False)

        context = browser.new_context(
            user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
        context.clear_cookies()
        page = context.new_page()
        logger.info(f"devices: {p.devices}")
        page.goto(url)
        page.wait_for_load_state("load")
        # page.wait_for_timeout(10000)
        content = page.inner_text("body")

        # 保持浏览器打开，等待用户手动关闭
        logger.info("页面内容已获取完成，按Ctrl+C退出...")
        try:
            # 无限循环等待Ctrl+C
            while True:
                time.sleep(10)
        except KeyboardInterrupt:
            logger.info("收到退出信号，正在关闭浏览器...")
            browser.close()
            logger.info("浏览器已关闭，程序退出")
